# encoding:utf-8
import sys
import json

sys.path.append("..")
from Orm.model import Model
from Orm.crud_builder import Where
from Orm.query_builder import QueryBuilder


class TestModel(Model):
    table = 'test'

    def __init__(self):
        Model.__init__(self)


def test_query():
    return TestModel().set_debug(True).where("name", '=', 3).where('type', '<>', 'teacher').where([
        [Where.TYPE_NULL, 'id', False],
        [Where.TYPE_BETWEEN, 'age', [18, 20], False],
        [Where.TYPE_NEST, [
            [Where.TYPE_NULL, 'id', False],
            [Where.TYPE_BETWEEN, 'age', [18, 20], False],
        ], False]
    ]).join_s("user", [Where.TYPE_SIMPLE, "test.user_id", '=', 'user.id', True]) \
        .select(['id', 'name', 'key']) \
        .order_by({"ctime": 'desc'}) \
        .first()


def test_update():
    return TestModel().set_debug(True).set_sql_type(QueryBuilder.TYPE_UPDATE).where("name", '=', 3).where('type', '<>',
                                                                                                          'teacher').where(
        [
            [Where.TYPE_NULL, 'id', False],
            [Where.TYPE_BETWEEN, 'age', [18, 20], False],
            [Where.TYPE_NEST, [
                [Where.TYPE_NULL, 'id', False],
                [Where.TYPE_BETWEEN, 'age', [18, 20], False],
            ], False]
        ]) \
        .update({"xxxx": "bbb", "bbbb": 2})


def test_delete():
    return TestModel().set_debug(True).set_sql_type(QueryBuilder.TYPE_DELETE).where("name", '=', 3).where('type', '<>',
                                                                                                          'teacher').where(
        [
            [Where.TYPE_NULL, 'id', False],
            [Where.TYPE_BETWEEN, 'age', [18, 20], False],
            [Where.TYPE_NEST, [
                [Where.TYPE_NULL, 'id', False],
                [Where.TYPE_BETWEEN, 'age', [18, 20], False],
            ], False]
        ]).delete()


def test_insert():
    return TestModel().set_debug(True).set_sql_type(QueryBuilder.TYPE_INSERT).insert({"xxxx": "bbb", "bbbb": 2})


if __name__ == '__main__':
    print test_query()

    print test_update()

    print test_delete()

    print test_insert()
